package com.banciyuan.bcywebview.base.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;


public class ConfirmDialog extends Dialog {
    public ConfirmDialog(Context context) {
        super(context);
    }

    public ConfirmDialog(Context context, int theme) {
        super(context, theme);
    }

    protected ConfirmDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context mContext;
        private TextView tvDialogDesc, tvDialogAction, tvDialogCancel;
        private String descString, actionString, cancelString;
        private View.OnClickListener actionClickListener, cancelClickListener;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setDescString(String descString) {
            this.descString = descString;
            return this;
        }

        public Builder setActionString(String actionString) {
            this.actionString = actionString;
            return this;
        }

        public Builder setCancelString(String cancelString) {
            this.cancelString = cancelString;
            return this;
        }

        public Builder setActionClickListener(View.OnClickListener actionClickListener) {
            this.actionClickListener = actionClickListener;
            return this;
        }

        public Builder setCancelClickListener(View.OnClickListener cancelClickListener) {
            this.cancelClickListener = cancelClickListener;
            return this;
        }

        public ConfirmDialog create() {
            View layout = LayoutInflater.from(mContext).inflate(R.layout.confirm_dialog_layout, null);
            ConfirmDialog confirmDialog = new ConfirmDialog(mContext, R.style.PcHeadDialog);
            confirmDialog.setContentView(layout);
            initView(layout);
            initData();
            initAction(confirmDialog);
            setBackgroudClick(layout, confirmDialog);
            return confirmDialog;
        }

        private void initAction(final ConfirmDialog confirmDialog) {
            tvDialogAction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != actionClickListener)
                        actionClickListener.onClick(v);
                    confirmDialog.dismiss();
                }
            });
            tvDialogCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != cancelClickListener)
                        cancelClickListener.onClick(v);
                    confirmDialog.dismiss();
                }
            });
        }

        private void initData() {
            tvDialogDesc.setText(descString);
            tvDialogAction.setText(actionString);
            tvDialogCancel.setText(cancelString);
        }

        private void initView(View layout) {
            tvDialogDesc = (TextView) layout.findViewById(R.id.tv_dialog_desc);
            tvDialogAction = (TextView) layout.findViewById(R.id.tv_dialog_action);
            tvDialogCancel = (TextView) layout.findViewById(R.id.tv_dialog_cancel);
        }

        private static void setBackgroudClick(View layout, final ConfirmDialog confirmDialog) {
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    confirmDialog.dismiss();
                }
            });
        }
    }
}
